﻿using System;
using Core.Authenticated;
using Core.Library;
using DataObject.Order;
using DataObject.OrderDetail;

namespace CMS.Controls.Order
{
    public partial class OrderDetail : AuthenticatedControl
    {
        protected int OrderId;
        protected string Discount;
        protected string TotalValue;
        protected string Freight;
        protected string LiquidationType;
        protected string OrderDate;
        protected string RequiredDate;
        protected string AdminUser;
        protected string OrderStatus;

        protected void Page_Load(object sender, EventArgs e)
        {
            OrderId = Utils.GetIdFromRequestString(Utils.OrderId, 0);

            if (!IsPostBack)
            {
                if (OrderId > 0)
                {
                    GetOrder(OrderId);
                    GetOrderDetailsInfo();
                    ListOrderDetailByOrderId();
                }
            }
        }

        private void GetOrderDetailsInfo()
        {
            var message = "";
            if (ConvertUtility.ToInt32(Freight) > 0)
                message += string.Format("<tr><td colspan=\"5\" class=\"alignright\">Phí vận chuyển: {0}<span class=\"red\" style=\"font-weight: bold\">(Tính cho cửa hàng)</span></td></tr>", Freight);

            message += string.Format("<tr><td colspan=\"5\" class=\"alignright\">Tổng: <span class=\"green\"  style=\"font-weight: bold\">{0}</span></td></tr>", TotalValue);

            ltOrderDetails.Text = message;
        }

        private void ListOrderDetailByOrderId()
        {
            var orderDetails = OrderDetailController.OrderDetail_GetListByOrderId(OrderId);
            rptOrderDetail.DataSource = orderDetails;
            rptOrderDetail.DataBind();
        }

        protected void GetOrder(int orderId)
        {
            var o = OrderController.Order_GetById(orderId);
            if (o != null)
            {
                TotalValue   = Utils.FormatPrice(ConvertUtility.ToDecimal(o.TotalValue));
                OrderDate    = DateTimeFunctions.FormatIn_DDMMYYYY(o.OrderDate);
                RequiredDate = DateTimeFunctions.FormatIn_DDMMYYYY(o.RequiredDate);
                OrderStatus  = OrderController.GetOrderStatusName(ConvertUtility.ToInt32(o.Status));
            }
        }
    }
}